/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.institution.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.institution.bean.InstitutionReviseAssessment;
import com.koron.institution.service.api.ReviseAssessmentService;
import com.koron.institution.uitls.BusinessCode;
import com.koron.institution.uitls.DateUtil;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "ReviseAssessment", description = "评估修订Api类")
@RestController
@RequestMapping(value = "/reviseAssessment")
public class ReviseAssessmentController {

    @Autowired
    private ReviseAssessmentService reviseAssessmentService;

    @ApiOperation("获取评估修订列表")
    @RequestMapping(value = "/getAssessmentList.htm", method = {RequestMethod.GET})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "reviseDescription", value = "修订描述", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", dataType = "Integer")
    })
    public MessageBean<?> getAssessmentList(String corpCode, String reviseDescription, Integer page, Integer pageSize,
                                            @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (null==user){
            MessageBean<?> msg=new MessageBean<>();
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        return reviseAssessmentService.getAssessmentList(corpCode, reviseDescription, page, pageSize, user);
    }


    @ApiOperation("保存修订评审")
    @RequestMapping(value = "/saveOrCommit.htm", method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "修订描述", name = "reviseDescription", required = true),
            @ApiImplicitParam(value = "修订原因", name = "reviseReason", required = true),
            @ApiImplicitParam(value = "版本号", name = "version", required = true),
            @ApiImplicitParam(value = "记录编号", name = "code"),
            @ApiImplicitParam(value = "评审修订Id", name = "id"),
            @ApiImplicitParam(value = "归档日期", name = "fileDateStr", required = true),
            @ApiImplicitParam(value = "文件名称", name = "fileName", required = true),
            @ApiImplicitParam(value = "水司code", name = "corpCode", required = true),
            @ApiImplicitParam(name = "operationType", value = "操作类型：0.草稿，1.提交", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "keepFileAddress", value = "原附件剩余url", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "delAttAddress", value = "删除的文件剩余url", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "approverCodeListStr", value = "审批人code集合字符串", required = true, paramType = "query", dataType = "String"),
    })
    @Log(title = "保存修订评审",businessModule = "评估修订", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> saveOrCommit(String id, String reviseDescription, String reviseReason, String version, String code,
                                       String fileDateStr, String fileName, String corpCode,
                                       @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                       Integer operationType, String keepFileAddress, String delAttAddress, @RequestBody MultipartFile[] files,
                                       String approverCodeListStr) {
        InstitutionReviseAssessment institutionReviseAssessment = new InstitutionReviseAssessment();
        institutionReviseAssessment.setReviseDescription(reviseDescription);
        institutionReviseAssessment.setReviseReason(reviseReason);
        institutionReviseAssessment.setVersion(version);
        institutionReviseAssessment.setCode(code);
        institutionReviseAssessment.setFileDate(DateUtil.formatStringToDate(fileDateStr, DateUtil.DATE_FORMAT_YMD));
        institutionReviseAssessment.setFileName(fileName);
        institutionReviseAssessment.setCorpCode(corpCode);
        institutionReviseAssessment.setId(id);
        return reviseAssessmentService.saveOrCommit(user, institutionReviseAssessment, operationType, keepFileAddress, files, approverCodeListStr);
    }

    @ApiOperation("修订文件导出")
    @RequestMapping(value = "/exports.htm", method = {RequestMethod.GET})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDateStr", value = "起始日期", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDateStr", value = "结束日期", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司编号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "水司名称", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "institutionType", value = "制度法规类型", paramType = "query", dataType = "Integer"),
    })
    public HttpEntity<?> exports(String startDateStr, String endDateStr, String corpCode, String corpName, Integer institutionType, @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        Map<String, Object> map = new HashMap<>();
        map = reviseAssessmentService.queryInstitution(startDateStr, endDateStr, corpCode, corpName, institutionType, user);
        if (map == null) {
            return null;
        }
        File targetFile = new File("static/excel/ReviseAssessment.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/ReviseAssessment.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            return null;
        }
        return Excel.createFile("评估修订列表", targetFile, "ReviseAssessment.btl", map);
    }


    @ApiOperation("流程中的业务数据")
    @ApiImplicitParam(name = "ids", value = "流程关联的业务ids", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/getProcessData.htm", method = {RequestMethod.GET})
    public MessageBean<?> getProcessData(String ids) {
        return reviseAssessmentService.getProcessData(ids);
    }


}
